package dk.itu.samd.group2.tagapastry;

import dk.itu.samd.group2.tagapastry.messages.ReplicationMsg;
import rice.p2p.commonapi.NodeHandle;

/*
 * Simple replication startegy to sync data between
 * nodes in a ring
 */
public class SimpleReplicationStrategy implements ReplicationStrategy {

	public SimpleReplicationStrategy() {;}
	
	/*
	 * (non-Javadoc)
	 * @see dk.itu.samd.group2.tagapastry.ReplicationStrategy#syncData(dk.itu.samd.group2.tagapastry.Tagapastry, rice.p2p.commonapi.NodeHandle, boolean)
	 */
	public void syncData(TagapastryImpl taga, NodeHandle handle, boolean result) {
		
		if(result) {
			if(taga.getIndexes().size() > 0)
			{
				// Okay node joined the ring, we need to send him our indexes.				
				// Create a msg to it with source's knowledge about indexes
				Tag tag = TagFactory.generateTag("", "");
				// Remember to overwrite getRecipientId for the replication msg
				NodeState ns = new NodeState(taga.getIndexes(), taga.getRetractionThreshold(), taga.getExpansionThreshold());
				ReplicationMsg rep = new ReplicationMsg(tag, taga.getNodeHandle(), handle,ns);
				taga.send(rep);
			}
			else {
				//System.out.println("Sorry no indexes available to sync");
			}
		} else {	// Just for debugging
			//System.out.println("Node left the ring");
		}
		
	}

}
